"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.intervalTimes = intervalTimes;
exports.excuteIntervalRightNow = excuteIntervalRightNow;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

function intervalTimes() {
  var times = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
  var func = arguments.length > 1 ? arguments[1] : undefined;

  for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
    args[_key - 2] = arguments[_key];
  }

  return new Promise(function (resolve, reject) {
    var time = 1;
    var intervalIns = setInterval.apply(void 0, [
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      var res,
          _args = arguments;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (!(time <= times)) {
                _context.next = 7;
                break;
              }

              _context.next = 3;
              return func.apply(void 0, _args);

            case 3:
              res = _context.sent;

              if (res) {
                clearInterval(intervalIns);
                resolve.apply(void 0, _args);
              } else {
                time++;
              }

              _context.next = 9;
              break;

            case 7:
              reject.apply(void 0, _args);
              clearInterval(intervalIns);

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, this);
    }))].concat(args));
  });
}

function excuteIntervalRightNow() {
  var runFunc = arguments[0];
  runFunc();
  return setInterval(runFunc, arguments.slice(1));
}